package com.truckdispatching.push;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class PushService extends Service {

	private static final long CHECK_TIME =  30*60*1000;
	private AlarmManager mAlarmManager;
	private PendingIntent mAlarmIntent;

	private final int HALF_HOUR = 1;

	@Override
	public void onCreate() {
		mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

		Intent iBroadcastIntent = new Intent(this, PushReciver.class);
		iBroadcastIntent.setAction("com.truckdispatching.push");
		mAlarmIntent = PendingIntent.getBroadcast(this, HALF_HOUR, iBroadcastIntent,
					Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);

		mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), CHECK_TIME, mAlarmIntent);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onDestroy() {
		if (mAlarmManager != null && mAlarmIntent != null) {
			mAlarmManager.cancel(mAlarmIntent);
		}
		super.onDestroy();
	}
}
